<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: install.joomleague.php 1235 2008-07-18 13:04:34Z julienv $
 * @package Joomleague  
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 

/**
 * Creates a folder, displays error in case of failure
 *
 * @param string $folder_path full path to create
 * @return bool TRUE on success or FALSE on failure.
 */
function create_folder( $folder_path )
{
	if ( !file_exists( $folder_path ) )
	{
		if( mkdir( $folder_path, 0777 ) )
		{
			print "<font color=green>" . $folder_path . " successfully created!</font><br />";
		}
		else {
			print "<font color=red>" . $folder_path . " creation failed, please do it manually!</font><br />";
			return false;
		}
	}
	else {
		print "<font color=green>" . $folder_path . " already exists !</font><br />";
	}
	return true;
}

function full_copy( $source, $target )
{
	if ( is_dir( $source ) )
	{
		@mkdir( $target );
		 
		$d = dir( $source );
		 
		while ( FALSE !== ( $entry = $d->read() ) )
		{
			if ( $entry == '.' || $entry == '..' )
			{
				continue;
			}
			 
			$Entry = $source . '/' . $entry;
			if ( is_dir( $Entry ) )
			{
				full_copy( $Entry, $target . '/' . $entry );
				continue;
			}
			copy( $Entry, $target . '/' . $entry );
		}
		 
		$d->close();
	}
	else
	{
		copy( $source, $target );
	}
}
    
function com_install() 
{
  global $mosConfig_live_site, $database, $mosConfig_absolute_path, $mosConfig_lang, $_VERSION;
  DEFINE('_JL_OK','O.K.');
  //change menu icon
	$database->setQuery( " SELECT id 
	                       FROM #__components 
	                       WHERE admin_menu_link = 'option=com_joomleague'" );
	$id = $database->loadResult();

	//add new admin menu images
	$database->setQuery( "UPDATE #__components " .
                        "SET admin_menu_img  = '../components/com_joomleague/images/joomleague/joomleague_icon.png'" .
                        ",   admin_menu_link = 'option=com_joomleague' " .
                        "WHERE id='$id'");
	$database->query();
	
	?>
	<h1 align="center"><strong>&nbsp;JoomLeague League Management and Prediction Game for Joomla!</strong></h1>
	<p>&nbsp;</p>
	<div style="text-align: center">
	<img src="<?php echo $mosConfig_live_site;?>/components/com_joomleague/images/joomleague/joomleague_logo.png" alt="JoomLeague Logo" title="JoomLeague Logo" width="410" height="200" />
	</div>
	<br /><br />
	<h4 style="color: green" align="center">JoomLeague 0.93 was installed !</h4>
	<link href="<?php echo $mosConfig_live_site; ?>/administrator/components/com_joomleague/css/joomleague_admin.css" rel="stylesheet" type="text/css" />
	<?php 
	
  /*
   * run the update scripts in case of upgrade
   */ 
  $versions_history = array('0.85', '0.86', '0.86a', '0.90', '0.91', '0.92', '0.93');
  
  include_once( $mosConfig_absolute_path.'/administrator/components/com_joomleague/classes/Version.class.php' );
  include_once( $mosConfig_absolute_path.'/administrator/components/com_joomleague/classes/xml.class.php' );
  
  $jl_v = new JoomLeagueVersion();
  $installing_version = $jl_v->version;
  
  $database->setQuery( "SELECT version FROM #__joomleague_version Order by id desc LIMIT 1" ); 
  $current_version = $database->loadResult();
  
  if (!$current_version) { 
  	// check that we are not upgrading from a version prior to 0.91... 
  	// To do that, we compare fields present in joomleague table 
  	// with fields that were added/remove in the different versions
  	$fields = $database->getTableFields( array('#__joomleague') );
  	$joomleague_table_fields = array_keys($fields['#__joomleague']);
  	
  	if ( (!in_array('current_round_auto', $joomleague_table_fields))
  	   && in_array('joomleague_editor', $joomleague_table_fields) ) {
  	  $current_version = '0.85'; 
  	}
    else if ((!in_array('show_match_id_results', $joomleague_table_fields))
           &&  in_array('current_round_auto', $joomleague_table_fields)
           &&  in_array('plan_layout', $joomleague_table_fields)) {
      $current_version = '0.86'; 
    }
    else if (in_array('show_match_id_results', $joomleague_table_fields)) {
      $current_version = '0.86a'; 
    }
    else if ((!in_array('project_type', $joomleague_table_fields))
           &&  in_array('use_legs', $joomleague_table_fields) ) {
      $current_version = '0.90'; 
    }
  	else echo "<p>IMPORTANT: If you are upgrading from a version prior to 0.85, please run the update scripts in maintenance menu manually</p>";
  }
  
  if ($current_version) {	// it is not the first time the component is installed
    echo "upgrading from: $current_version<br>";
  	if ($current_version != $installing_version) { 
  		//this is a new version
	  	while ($current_version != $installing_version) {
	      $next_version = $versions_history[ array_search($current_version, $versions_history)+1 ];
		  	$auto_update = 1;
	      include($mosConfig_absolute_path.'/administrator/components/com_joomleague/updates/update_'.$current_version.'_to_'.$next_version.'.php');
	      echo "updated database to version $next_version<br/>";
	      $current_version = $next_version;
	    }
	    // register new version
	    $database->setQuery( "INSERT INTO #__joomleague_version (version) VALUES ('".$installing_version."')" );
	    $database->query();
	    echo "Registered version $installing_version in database<br/>";
  	}
  	else {
  	  //reinstalling same version, happens with beta...
  	  $previous_version = $versions_history[ array_search($current_version, $versions_history)-1 ];
      $auto_update = 1;
      include($mosConfig_absolute_path.'/administrator/components/com_joomleague/updates/update_'.$previous_version.'_to_'.$current_version.'.php');
      echo "Reran update script from version $previous_version to $current_version<br/>";
  	}
	}
  else { //first install of the component, register the version
  	$database->setQuery( "INSERT INTO #__joomleague_version (version) VALUES ('".$installing_version."')" );
    $database->query();
    echo "Registered version $installing_version in database<br/>";
  }
  
  /*
   * images folder
   */
  echo "Now creating image folder<br/>";
  if( is_writable( $mosConfig_absolute_path . "/images/" ) ) 
  {
    $placeholdersFiles = array("placeholder_50.png"
			,"placeholder_150.png"
			,"placeholder_450.png"
			,"placeholder_small.gif"); 
	  
		/*
		 * Creates images folders
		 */
		$jl_img_path = $mosConfig_absolute_path . "/images/joomleague";
		if ( file_exists( $jl_img_path ) || create_folder( $jl_img_path ) )
		{
	    if ( !file_exists( $jl_img_path . "/placeholders/" ) ) {
        create_folder( $jl_img_path . "/placeholders/" );
	    }
      if ( !file_exists( $jl_img_path . "/teams/" ) ) {
        create_folder( $jl_img_path . "/teams/" );
      }
      if ( !file_exists( $jl_img_path . "/event_icons/" ) ) {
        create_folder( $jl_img_path . "/event_icons/" );
      }
	  }
	  
	  // copy images
	  foreach ( $placeholdersFiles AS $placeholdersFile ) 
	  {
		  if ( copy( $mosConfig_absolute_path . "/components/com_joomleague/images/placeholders/".$placeholdersFile,
		             $mosConfig_absolute_path . "/images/joomleague/placeholders/".$placeholdersFile ) ) {
        print "<font color=green>".$placeholdersFile." Successfully added to the placeholders!</font><br />";
		   } 
		  else {
				print "<font color=red>".$placeholdersFile." Failed to be added to the placeholders please do so manually!</font><br />";
			}
	  }
	  
	  /*
	   * copy events icons
	   */
    full_copy( $mosConfig_absolute_path . '/components/com_joomleague/frontend/default/images/event_icons',
               $jl_img_path . '/event_icons/' );
  }
	else {
		print "<font color=red>".$mosConfig_absolute_path . "/images/ is not writable!<br />  Manually do the following:<br /> 1.) create ".$mosConfig_absolute_path . "/images/joomleague/ directory <br /> 2.) chmod it to 777 <br /> 3.) create ".$mosConfig_absolute_path . "/images/joomleague/placeholders/ <br /> 4.) chmod it to 777 <br />5.) copy ".$mosConfig_absolute_path . "/components/com_joomleague/images/placeholders/ and its contents to ".$mosConfig_absolute_path . "/images/joomleague/placeholders/  </font><br />";
	}
	  
	?>
	<p align="center">
	<b>Please have a look at the <a href="http://www.joomleague.de" target="_blank" title="JoomLeague manual">Manual pages</a>
	at www.joomleague.de and get latest informations on joomleague from our <a href="http://www.joomleague.de/forum" target="_blank" title="JoomLeague Forum">Forum</a>.
	</b></p>
	<?php if ($_VERSION->RELEASE != '1.0'): ?>
	<p style="color:red; font-weight:bold">
	WARNING: you are not installing on joomla 1.0.X ! Event in legacy mode, there could be some issues not supported by this version of Joomleague on this version of Joomla.
	</p>
	<?php endif; ?>
	<br />
	<hr />
	<p align="center">
	 <em>JoomLeague is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	Please note that the GPL states that any headers in files and
	Copyright notices as well as credits in headers, source files
	and output (screens, prints, etc.) can not be removed.
	You can extend them with your own credits, though...
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
	
	The "GNU General Public License" (GPL) is available at
	http://www.gnu.org/copyleft/gpl.html.</em>
	</p>
	<hr />
	<br />
	<p align="center">Visit the project page, if you need help, and/or if you want to say how much you like it... 
	Or if you want to help us !
	</p>
	<p align="center">&nbsp;Nun viel Spa&szlig; mit JoomLeague und denkt daran 
	JoomLeague lebt vom mitarbeiten, wenn euch Verbesserungen einfallen oder Ihr 
	bei der Fehlerbehebung helfen k&ouml;nnt, meldet euch im JoomLeague Forum.
	<br />&nbsp;<br />&copy; 2007 Joomleague.de
	</p>
	<?php	
}
?>
